package com.lianbang.staff.module.main

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import com.lianbang.staff.R
import com.lianbang.staff.adapter.ChatListAdapter
import com.lianbang.staff.bean.ChatListBean
import com.lianbang.staff.databinding.ActivityChatBinding
import com.lianbang.staff.vm.MainVM
import kotlinx.android.synthetic.main.activity_chat.*

class ChatActivity : BaseActivity<MainVM, ActivityChatBinding>() {

    private val mAdapter by lazy {
        ChatListAdapter()
    }

    private val mLinearLayoutManager by lazy {
        LinearLayoutManager(this)
    }

    companion object {
        fun start(context: Context?) {
            context?.also {
                val starter = Intent(it, ChatActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                it.startActivity(starter)
            }

        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_chat
    }

    override fun initData(intent: Intent) {

    }

    override fun setToolbarTitle(): String? {
        return "聊天页"
    }

    override fun initView() {
        rv_list.layoutManager = mLinearLayoutManager
        rv_list.adapter = mAdapter

        mAdapter.replaceData(
            mutableListOf(
                ChatListBean(1),
                ChatListBean(2),
                ChatListBean(1),
                ChatListBean(1),
                ChatListBean(2),
                ChatListBean(1),
                ChatListBean(2),
                ChatListBean(1),
                ChatListBean(2),
                ChatListBean(2),
                ChatListBean(1)
            )
        )

        btn_submit.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                val input = et_input.text.toString()
                if (input.isNotEmpty()) {
                    mAdapter.addData(ChatListBean(2))
                }
            }

        })
    }

    override fun loadData() {

    }

    /**
     * 滑到最新位置
     */
    fun scrollBottom() {
        Handler().postDelayed(
            Runnable { mLinearLayoutManager.scrollToPositionWithOffset(mAdapter.data.size - 1, 0) },
            100
        )
    }
}
